package data;

import constants.Colors;
import constants.Races;

import javax.swing.*;
import java.awt.*;
import java.util.HashMap;
import java.util.LinkedList;

/**
 * Player Class
 * Holds the information for a player
 *
 * @author Jared Moore
 * @date 10/5/13
 */
public class Player
{
	private int money;
	private String name;
	private Races race;
	private Colors color;
	private LinkedList<Tile> properties;
	private final int[] resources;
	private boolean isUser;
	private MULE mule;
	public static final HashMap<Colors, Color> COLORS = new HashMap<>();
	public static final HashMap<Colors, String> COLOR_NAMES = new HashMap<>();
	public static final HashMap<Races, String> RACE_NAMES = new HashMap<>();
	public final ImageIcon ICON;

	static
	{
		COLORS.put(Colors.BLUE, Color.BLUE);
		COLORS.put(Colors.GREEN, Color.GREEN);
		COLORS.put(Colors.RED, Color.RED);
		COLORS.put(Colors.YELLOW, Color.YELLOW);
		COLORS.put(Colors.ORANGE, Color.ORANGE);
		COLORS.put(Colors.PINK, Color.PINK);

		COLOR_NAMES.put(Colors.BLUE, "blue");
		COLOR_NAMES.put(Colors.GREEN, "green");
		COLOR_NAMES.put(Colors.RED, "red");
		COLOR_NAMES.put(Colors.YELLOW, "yellow");
		COLOR_NAMES.put(Colors.ORANGE, "orange");
		COLOR_NAMES.put(Colors.PINK, "pink");

		RACE_NAMES.put(Races.BONZOID, "Bonzoid");
		RACE_NAMES.put(Races.BUZZITE, "Buzzite");
		RACE_NAMES.put(Races.FLAPPER, "Flapper");
		RACE_NAMES.put(Races.HUMAN, "Human");
		RACE_NAMES.put(Races.UGAITE, "Ugaite");
	}

	public Player(String name, Races race, Colors color, int[] resources, boolean isUser)
	{
		this.name = name;
		this.race = race;
		this.color = color;
		properties = new LinkedList<>();
		this.isUser = isUser;
		this.resources = resources;

		switch (race)
		{
			case HUMAN:
				money = 600;
				break;
			case FLAPPER:
				money = 1600;
				break;
			default:
				money = 1000;
				break;
		}

		String iconString = "res/" + COLOR_NAMES.get(color) + "/" + RACE_NAMES.get(race) + ".png";
		ICON = new ImageIcon(iconString);
	}

	public void addProperty(Tile property)
	{
		properties.add(property);
        property.setOwner(this);
	}

	public boolean decrementFood(int amount)
	{
		if (amount > resources[0])
		{
			resources[0] -= amount;
			return false;
		}
		else
		{
			resources[0] -= amount;
			return true;
		}
	}

	public boolean decrementEnergy(int amount)
	{
		if (amount > resources[1])
		{
			return false;
		}
		else
		{
			resources[1] -= amount;
			return true;
		}
	}

	public boolean decrementOre(int amount)
	{
		if (amount > resources[2])
		{
			return false;
		}
		else
		{
			resources[2] -= amount;
			return true;
		}
	}

	public void addFood(int amount)
	{
		resources[0] += amount;
	}

	public void addEnergy(int amount)
	{
		resources[1] += amount;
	}

	public void addOre(int amount)
	{
		resources[2] += amount;
	}

	public int getMoney()
	{
		return money;
	}

	public boolean makePurchase(int cost)
	{
		if (cost > money)
		{
			return false;
		}
		else
		{
			money -= cost;
			return true;
		}
	}

	public String getName()
	{
		return name;
	}

	public Races getRace()
	{
		return race;
	}

	public Color getColor()
	{
		return COLORS.get(color);
	}

	public LinkedList<Tile> getProperties()
	{
		return properties;
	}

	public boolean isUser()
	{
		return isUser;
	}

	public void setUser(boolean user)
	{
		isUser = user;
	}

	public boolean hasFreeMule()
	{
		return false;
	}

	public MULE getFreeMule()
	{
		return mule;
	}

	@Override
	public String toString() {
		return name;
	}

	public int getFood()
	{
		return resources[0];
	}
}
